/**
 * 加密解密
 * 2017年1月14日
 */

/** RSA加密用 生成key */
function bodyRSA(){
	/** 1024位的key参数写130,2014位的key参数写260 */
	setMaxDigits(130);
	/** RSAKeyPair 函数三个参数：加密指数、解密指数、系数 */
	return new RSAKeyPair("10001","","MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTgtStXylgS8FS3Yr/TSVGy0crIUqMFcwqyWg0FvsM7H4NSGSenkMzGwDJACkjE2KxNylRudyq2nU63wV64X2Dq1OZ+sHB9qR6FWLNNKF9nlDa2zO0nhkA+Q4EIMUtqoKFnCc+Rn9bu6HaGN555o0srUkqVjiFD+0m6kp05Zf8NwIDAQAB");
}

/** AES加密用 随机生成key和iv */
function randomString() {
	var chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	var length= chars.length;
	var pwd='';
	for(var i = 0; i < 16 ;i++){
		pwd += chars.charAt(Math.floor(Math.random() * length));
	}
	return pwd;
}
/**
 * AES加密
 * @param data
 * @param key
 * @param iv
 * @returns
 */
function getAesString(data,key,iv){
    var key  = CryptoJS.enc.Utf8.parse(key);
    var iv   = CryptoJS.enc.Utf8.parse(iv);
    var encrypted = CryptoJS.AES.encrypt(data,key,
            {
                iv:iv,
                mode:CryptoJS.mode.CBC,
                padding:CryptoJS.pad.Pkcs7
            });
    return encrypted.toString();
}
/**
 * AES解密
 * @param encrypted
 * @param key
 * @param iv
 * @returns
 */
function getDAesString(encrypted,key,iv){
    var key  = CryptoJS.enc.Utf8.parse(key);
    var iv   = CryptoJS.enc.Utf8.parse(iv);
    var decrypted = CryptoJS.AES.decrypt(encrypted,key,
            {
                iv:iv,
                mode:CryptoJS.mode.CBC,
                padding:CryptoJS.pad.Pkcs7
            });
    return decrypted.toString(CryptoJS.enc.Utf8);
}